<template>
  <div>
    nc_ids: {{ nc_ids }}
    <a-button v-if="nc_ids.length" @click="nc2ods"> nc2ods </a-button>
    <!-- <div>ODS: {{ records }}</div> -->

    <a-table
      :row-selection="rowSelection"
      :columns="columns"
      :data-source="records"
      rowKey="id"
    >
    </a-table>

    <a-button @click="selectSome"> selectSome </a-button>
  </div>
</template>

<script>
import api from '@/odoorpc'

export default {
  name: 'NcTreeView',
  components: {},
  mixins: [],
  props: {
    nc_ids: { type: Array, default: () => [] }
  },

  data() {
    return {
      records: [],
      activeIds: []
    }
  },

  computed: {
    columns() {
      return [
        {
          dataIndex: 'name',
          key: 'name',
          title: '订单编号',
          ellipsis: true
        },
        {
          dataIndex: 'client_order_ref',
          key: 'client_order_ref',
          title: 'NC发票PK',
          ellipsis: true
        },
        {
          dataIndex: 'invoice_num',
          key: 'invoice_num',
          title: 'NC发票号',
          ellipsis: true
        },
        {
          dataIndex: 'biz_type',
          key: 'biz_type',
          title: '客户代码',
          ellipsis: true
        },
        {
          dataIndex: 'company_id',
          key: 'company_id',
          title: '公司ID',
          ellipsis: true
        },
        {
          dataIndex: 'partner_id',
          key: 'partner_id',
          title: '客户',
          ellipsis: true
        }
      ]
    },

    rowSelection() {
      return {
        selectedRowKeys: this.activeIds,
        onChange: (selectedRowKeys, selectedRows) => {
          // console.log(selectedRowKeys, selectedRows)
          this.activeIds = selectedRowKeys
          // this.handleOnRowSelect(selectedRowKeys, selectedRows)
        }
      }
    }
  },

  created() {
    // console.log('this is creatd')
  },

  mounted() {
    this.nc2ods()
    // console.log('mounted', this.$route.fullPath)
  },

  methods: {
    async nc2ods() {
      const ods_ids = await this._nc2ods(this.nc_ids)
      // console.log(ods_ids)
      const ods_records = await this._get_ods(ods_ids)
      this.records = ods_records
    },

    async _nc2ods(ids) {
      const Model = api.env.model('fp.nc.ods.sale.order')
      const ids2 = await Model.execute('load_to_model', ids)
      return ids2
    },

    async _get_ods(ods_ids) {
      const Model = api.env.model('fp.ods.sale.order')
      const fields = [
        'name',
        'client_order_ref',
        'invoice_num',
        'biz_type',
        'company_id',
        'partner_id'
      ]
      const records = await Model.read(ods_ids, fields)
      return records
    },

    selectSome() {
      //
      this.$emit(
        'select',
        this.records.map(item => item.id)
      )
    }
  }
}
</script>

<style type="text/css"></style>
